package com.cqut.forum.dao.imp;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.stereotype.Component;

import com.cqut.forum.dao.UserDao;
import com.cqut.forum.entity.User;

@Component
public class UserDaoImp extends BaseDaoImp implements UserDao{
	
	public boolean unDelete(int id) {
		User u = get(id, User.class);
		u.setUserState(false);
		return true;
	}

	@SuppressWarnings("unchecked")
	public User getUser(String userName, String password) {
		String hql = "select u from User u where u.userName = ? and u.userPassword = ?";
		List<Object> parameters = new ArrayList<Object>();
		parameters.add(userName);
		parameters.add(password);
		
		List<User> list = (List<User>) super.excuteHql(hql, parameters, null);
		return list.size()>0?list.get(0):null;
	}
	
	@SuppressWarnings("unchecked")
	public User getUser(String userName) {
		String hql = "select u from User u where userName=? and userState=true";
		Query query = super.getQuery(hql);
		query.setString(0, userName);
		
		List<User> list = query.list();
		if(list.size()==0){
			return null;
		}
		else
			return list.get(0);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getMostScore(int index, int size) {
		String sql = "select u.user_id,u.user_sex,u.user_name,u.user_photo,u.user_score from user u where u.user_state=true order by u.user_score desc";
		
		Map<String, Integer> pages = new HashMap<String, Integer>();
		pages.put("index", index);
		pages.put("size", size);
		
		List<Map<String, Object>> result = (List<Map<String, Object>>) super.excuteSql(sql, null, pages);
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public int getUserRank(int userId) {
		String sql = "select count(user_id) as rank from user where user_score>(" +
				"select u.user_score from user u where u.user_id=?)";
		List<Object> parameters = new ArrayList<Object>();
		parameters.add(userId);
		
		List<Map<String,Object>> list = (List<Map<String,Object>>) super.excuteSql(sql, parameters, null);
		BigInteger rank = (BigInteger) list.get(0).get("rank");
		return rank.intValue()+1;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<User> getFishUser(int index, int size) {
		String hql = "select u from User u where userState=true order by u.userTime desc";
		
		Map<String, Integer> pages = new HashMap<String, Integer>();
		pages.put("index", index);
		pages.put("size", size);
		
		List<User> result = (List<User>) super.excuteHql(hql, null, pages);
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getUsers(String userName,
			String userPlace, String registerTime, int index, int size) {
		String sql = "select u.user_id,u.user_name,u.user_place,u.user_sex,u.user_birthday," +
					 "u.user_time,u.user_last_login_time,u.user_state,r.role_name from " +
					 "user u,role r where u.role_id = r.role_id ";
					 //"user u,role r where u.role_id = r.role_id and u.user_name like \"%"+userName+"%\" and " +
					 //"u.user_place like \"%"+userPlace+"%\" ";
		if(!userName.equals("")){
			sql = sql + "and u.user_name like \"%"+userName+"%\" ";
		}
		if(!userPlace.equals("")){
			sql = sql + "and u.user_place like \"%"+userPlace+"%\" ";
		}
		if(null!=registerTime&&!"".equals(registerTime)){
			sql = sql + "and u.user_time>\""+registerTime+"\"";
		}
		//List<Object> parameters = new ArrayList<Object>();
		//parameters.add(registerTime);
		
		Map<String, Integer> pages = new HashMap<String, Integer>();
		pages.put("index", index);
		pages.put("size", size);
		
		List<Map<String, Object>> result = (List<Map<String, Object>>) super.excuteSql(sql, null, pages);
		return result;
	}

	@Override
	public boolean updateUserRole(int userId,int roleId) {
		String sql = "update user u set u.role_id="+roleId+" where u.user_id="+userId;
		
		Query query = super.getSqlQuery(sql);
		query.executeUpdate();
		return true;
	}
}
